#!/usr/bin/env python3
# -*- coding: utf-8 -*-

from flask import Flask, request, jsonify, send_from_directory
import os
import uuid

app = Flask(__name__)

# 配置文件存储路径
UPLOAD_FOLDER = '/root/car/avatar'
os.makedirs(UPLOAD_FOLDER, exist_ok=True)

@app.route('/upload', methods=['POST'])
def upload_file():
    """接收文件上传"""
    try:
        # 检查是否有文件上传
        if 'file' not in request.files:
            return jsonify({'error': '没有文件'}), 400
        
        file = request.files['file']
        if file.filename == '' or file.filename is None:
            return jsonify({'error': '没有选择文件'}), 400
        
        # 保存文件
        filename = file.filename or ''
        file_path = os.path.join(UPLOAD_FOLDER, filename)
        file.save(file_path)
        
        return jsonify({'message': '文件上传成功', 'filename': filename}), 200
    
    except Exception as e:
        return jsonify({'error': str(e)}), 500

@app.route('/avatar/<path:filename>')
def get_avatar(filename):
    """提供头像文件访问"""
    return send_from_directory(UPLOAD_FOLDER, filename)

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=8080, debug=True)